const fs = require('fs');
const path = require('path');

/**
 * 获取构建目标（默认 ServoTuner）
 */
console.log('💡 electron-builder config 加载中，BUILD_TARGET =', process.env.BUILD_TARGET);
const target = process.env.BUILD_TARGET || 'ServoTuner';

/**
 * 构建目标配置路径
 */
const targetConfigPath = path.resolve(__dirname, `config-${target}.json`);

if (!fs.existsSync(targetConfigPath)) {
  throw new Error(`构建配置不存在: ${targetConfigPath}`);
}

const variantConfig = JSON.parse(fs.readFileSync(targetConfigPath, 'utf-8'));

/**
 * 公共配置（适用于所有变体）
 */
const baseConfig = {
  directories: {
    app: 'release/app',
    output: `release/build/${target}`,
    buildResources: 'assets',
  },
  files: ['dist', 'node_modules', 'package.json', 'scripts', 'venv'],
  extraFiles: [
    {
      from: 'scripts/',
      to: 'scripts/',
      filter: ['**/*'],
    },
    {
      from: 'venv/',
      to: 'venv/',
      filter: ['**/*'],
    },
  ],
  extraResources: ['./assets/**'],
  asar: true,
  asarUnpack: '**\\*.{node,dll}',
  afterSign: '.erb/scripts/notarize.js',
  publish: {
    provider: 'github',
    owner: 'electron-react-boilerplate',
    repo: 'electron-react-boilerplate',
  },
};

module.exports = () => {
  return {
    ...baseConfig,
    ...variantConfig,
  };
};
